# -*- encoding: utf-8 -*-
import time
from Common.mysql import db
from Common.public_method import utc_8
from src.Bugs.Database.bugs_database import BugData
from Common.yaml_method import YamlMethod
from src.User.Database.user_database import User, UserSchema
from src.Bugs.Function.send_lark_massege import SendLarkMessage


class CreateBug:
    """
    创建缺陷
    """

    @staticmethod
    def create_bug(title, describe, project_id, sprint, severity, level, handler, responsible, bug_type, platform,
                   create_user):
        """
        创建缺陷
        :param title: 缺陷名称
        :param describe: 缺陷描述
        :param project_id: 项目ID
        :param sprint: 迭代
        :param severity: 严重程度
        :param level: 优先级
        :param handler: 处理人
        :param responsible: 责任人
        :param bug_type: 缺陷类型
        :param platform: 软件平台
        :param create_user: 创建人
        :return:
        """
        code = YamlMethod().read_data('code.yaml')['code']

        create_time = utc_8()
        # 缺陷数据插入数据库
        bug_info = BugData(title=title, describe=describe, projectId=project_id, sprint=sprint, severity=severity,
                           level=level, handler=handler, responsible=responsible, bugType=bug_type, platform=platform,
                           status='新', create_time=create_time,  create_user=create_user)
        db.session.add(bug_info)
        db.session.commit()
        # 查询用户飞书登录的open_id
        user = User.query.filter_by(realName=responsible).first()
        user_schema = UserSchema()
        user_data = user_schema.dump(user)
        open_id = user_data['openId']
        if open_id:
            # 发送飞书消息通知用户
            message_title = "新增一个bug通知"
            SendLarkMessage().send_lark_message(open_id, message_title, title, level, severity, create_user)

        res = {
            'code': code[0],
            'message': 'success',
            'data': {}
        }

        return res
